<?php

use console\db\Migration;

/**
 * 创建圈子相关信息表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m190102_180239_t_group extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 圈子信息表
         * @table `%group`
         */
        $this->tableBuilder([
            'name' => 'sns_group',
            'comment' => '圈子信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('名称'),
                'brand' => $this->char(255)->defaultValue('')->comment('图标'),
                'description' => $this->string(255)->defaultValue('')->comment('简介'),
                'type_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('分类'),
                'official' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('是否官方圈子'),
                'recommend_at' => $this->unixTimestamp()->comment('推荐时间'),
                'weight' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('权重'),
                'allow_format' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(3)->comment('允许的内容格式'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);


        /**
         * 圈子统计信息表
         * @table `%group_stats`
         */
        $this->tableBuilder([
            'name' => 'sns_group_stats',
            'comment' => '圈子统计信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('圈子ID'),
                'follows' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('关注数'),
                'threads' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('发帖数'),
                'comments' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论数'),
                'today_follows' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('今日关注数'),
                'today_threads' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('今日发帖数'),
                'today_comments' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('今日评论数'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'group_id',
                ]
            ]
        ]);

        /**
         * 圈子公告表
         * @table `%group_notice`
         */
        $this->tableBuilder([
            'name' => 'sns_group_notice',
            'comment' => '圈子公告表',
            'columns' => [
                'id' => $this->primaryKey(),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('圈子ID'),
                'title' => $this->string(64)->comment('公告标题'),
                'content' => $this->text()->notNull()->comment('公告内容'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('发布状态'),
                'expired_at' => $this->unixTimestamp()->comment('过期时间'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'group_id',
                ],
            ]
        ]);

        /**
         * 圈子分类表
         * @table `%group_type`
         */
        $this->tableBuilder([
            'name' => 'sns_group_type',
            'comment' => '圈子分类表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->comment('分类名称'),
                'icon' => $this->char(255)->comment('图标'),
                'listorder' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('排序'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 圈子访问日志表
         * @table `%group_access`
         */
        $this->tableBuilder([
            'name' => 'sns_group_access',
            'comment' => '圈子访问日志表',
            'columns' => [
                'id' => $this->primaryKey(),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('圈子ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'times' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('访问次数'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'group_id',
                ],
                [
                    'columns' => 'user_id',
                ],
            ]
        ]);

        /**
         * 圈子用户表
         * @todo create table `group_user`.
         */
        $this->tableBuilder([
            'name' => 'sns_group_user',
            'comment' => '圈子用户表',
            'columns' => [
                'id' => $this->primaryKey(),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('圈子ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'type' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('关系类型，0关注，-1黑名单，1管理员'),
                'liveness' => $this->integer(11)->notNull()->defaultValue(0)->comment('圈子内活力值'),
                'created_at' => $this->unixTimestamp()->comment('关注时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'group_id',
                ],
                [
                    'columns' => 'user_id',
                ],
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%sns_group_user}}');
        $this->dropTable('{{%sns_group_access}}');
        $this->dropTable('{{%sns_group_type}}');
        $this->dropTable('{{%sns_group_stats}}');
        $this->dropTable('{{%sns_group_notice}}');
        $this->dropTable('{{%sns_group}}');
    }

}
